package org.example.Controller;


import jakarta.annotation.Resource;
import org.example.pojo.Picture;
import org.example.service.PictureService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 图片文件管理的控制器类
 *
 * @author rjs
 */

@RestController
@RequestMapping("/picture")
public class PictureController {

    /**
     * 日志对象,用于记录日志
     */
    private static final Logger logger = LoggerFactory.getLogger(PictureController.class);

    /**
     * 图片文件服务对象
     */
    @Resource
    private PictureService pictureService;

    /**
     * 上传图片文件
     *
     * @param file 上传的图片文件
     * @return 保存是否成功
     */
    @PostMapping("/upload")
    public boolean uploadPicture(@RequestParam("file") MultipartFile file) {
        try {
            // 创建一个Picture对象，用于存储图片文件
            Picture picture = new Picture();
            picture.setPic(file.getBytes());
            // 调用PictureService的save方法，将图片文件保存到数据库
            return pictureService.save(picture);
        } catch (IOException e) {
            logger.error("上传图片时出错", e);
            return false;
        }
    }

    /**
     * 根据id查询图片文件
     *
     * @param id 图片文件id
     * @return 图片文件字节数组 with HTTP headers
     */
    @GetMapping("/{id}")
    public ResponseEntity<byte[]> getPictureById(@PathVariable Long id) {
        // 查询数据库中是否存在该ID的图片文件
        Optional<Picture> pictureOptional = Optional.ofNullable(pictureService.getById(id));
        if (pictureOptional.isPresent()) {
            Picture picture = pictureOptional.get();
            byte[] image = picture.getPic(); // 假设getPic()方法返回byte[]
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.IMAGE_JPEG);
            // 返回图片文件，包含HTTP头
            return new ResponseEntity<>(image, headers, HttpStatus.OK);
        } else {
            // 如果没有找到图片文件，返回HTTP 404 Not Found
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    /**
     * 更新图片文件
     *
     * @param picture 图片文件信息
     * @return 更新是否成功
     */
    @PutMapping
    public boolean updateById(@RequestBody Picture picture) {
        return pictureService.updateById(picture);
    }

    /**
     * 根据id删除图片文件
     *
     * @param id 图片文件id
     * @return 删除是否成功
     */
    @DeleteMapping("/{id}")
    public boolean removeById(@PathVariable Long id) {
        return pictureService.removeById(id);
    }

    /**
     * 查询所有图片文件id
     *
     * @return 图片文件id列表
     */
    @GetMapping
    public List<Long> listAll() {
        return pictureService.list().stream().map(Picture::getId).collect(Collectors.toList());
    }
}

